#!/bin/bash

set -e


if [ -d inst2 ]; then
    echo "The directory 'inst2' already exists, please delete it."
    exit 1
fi

# Build it
./master.sh mingw32 optimized

# Get the current version
VERSION=$(./configure -V | awk '/^Diamond Girl configure/{printf("%s\n", $4);}')

# Install it
make install

# Create temporary directory to hold the installation
mkdir inst2
cd inst2
mv ../inst/bin/diamond_girl.exe .
mv ../inst/share/diamond-girl/* .
sed 's/^[ ]*OutFile .*$/OutFile "diamond-girl-'${VERSION}'.exe"/' < ../diamond_girl-win32.nsi > diamond_girl-win32.nsi
convert themes/default/gfx/logo.png -background white -flatten themes/default/gfx/logo-win32installer.bmp
for i in \
    OpenAL32.dll        \
    SDL.dll             \
    SDL_image.dll       \
    glew32.dll          \
    libFLAC-8.dll       \
    libSDL_gfx-15.dll   \
    libbz2-1.dll        \
    libgcc_s_sjlj-1.dll \
    libjpeg-62.dll      \
    libjson-c-2.dll     \
    libmikmod-3.dll     \
    libogg-0.dll        \
    libpng16-16.dll     \
    libsndfile-1.dll    \
    libtiff-5.dll       \
    libvorbis-0.dll     \
    libvorbisenc-2.dll  \
    libvorbisfile-3.dll \
    zlib1.dll
do
    cp /usr/i686-w64-mingw32/sys-root/mingw/bin/${i} .
done


# And make the installer
makensis diamond_girl-win32.nsi
mv diamond-girl-${VERSION}.exe ../

# Cleanup
cd ..
rm -rf inst2
ls -l diamond-girl-${VERSION}.exe
